<?php
//登录设置
class Oauth2 {
    
    /**
     * 取得Oauth实例
     * @static
     * @return mixed 返回Oauth
     */
    public static function getInstance() {
        return new Oauth2();
    }
        
        
	/**
	 * 得到第三方登陆类型
	 */
	public function getOauthType(){
		return array('QQ','SINA','WEIXIN');
	}
	
	/**
	 * 登录
	 * @param string $type 第三方登录的类型
	 */
	function login($type){
	    redirect(self::loginURL($type));
	}
	
	
	/**
	 * 第三方登录URL
	 * @param string $type 第三方登录的类型
	 */
	function loginURL($type){
	    $type = strtoupper($type);
	    if(!in_array($type,$this->getOauthType()))
	        return false;
	    require_once(COM_DIR."Oauth/Oauth.class.php");
	    $instance = Oauth::getInstance($type);
	    return self::domainCheck($instance->getRequestCodeURL());
	}
	
	/**
	 * 检验当前域名是否与回调地址一致
	 * @param string $requestUrl 第三方登录URL
	 */
	private function domainCheck($requestUrl)
	{
	    if(!C('oauth_domain_check'))
	        return $requestUrl;
	    $arr = parse_url(urldecode($requestUrl));	    
	    $queryParts = explode('&', My::arr2var($arr,'query'));
	    $params = array();
	    foreach ($queryParts as $param)
	    {
	        $item = explode('=', $param);
	        if(isset($item[1]))
	            $params[$item[0]] = $item[1];
	    }
	    $redirect_uri = My::arr2var($params,'redirect_uri');
	    $domain = My::arr2var(parse_url($redirect_uri),'host');
	    if($_SERVER['HTTP_HOST']==$domain)
	        return $requestUrl;
	    else{
	        return __PROJECT__."member/oauth_error/domain-{$domain}/";
	    }
	        
	}
	
	
	/**
	 * 手机网站微信登录
	 */
	function weixinLogin(){
	    $type = 'WEIXIN';
	    require_once(COM_DIR."Oauth/Oauth.class.php");
	    $instance = Oauth::getInstance($type);
	    redirect(self::domainCheck($instance->getRequestCodeURL2()));
	}
	
	
	/**
	 * 处理返回信息
	 * @param string $type GET传入的登录类型type值
	 * @param string $code GET传入的code值
	 */
	function handleCallback($type,$code){
	    $type = strtoupper($type);
	    if(!in_array($type,$this->getOauthType()))
	        return false;
	    require_once(COM_DIR."Oauth/Oauth.class.php");
	    // 获取实例化对象
	    $sns = Oauth::getInstance($type);
	    // 给extend赋值初始null
	    $extend = null;
	    // 如果是腾讯微博的客户，此处给extend重新赋值
	    if ($type == 'TENCENT') {
	        $extend = array('openid' => $this->_get('openid'), 'openkey' => $this->_get('openkey'));
	    }
	    // 获取token值。
	    try{//执行
	        $token = $sns->getAccessToken($code, $extend);
	    }
	    catch(Exception $e){// $e是系统已经定义好的类
	        exit("错误：".$e->getMessage());
	    }
	    $openid = $token['openid'];
	    // 判断token是否是数组
	    if (is_array($token)) {
	        // 获取用户的详细信息
	        if($type=='QQ'){
	            $data = $sns->call('user/get_user_info');
	            return array('openid'=>$openid,'nickname'=>My::arr2var($data,'nickname'),'headimage'=>My::arr2var($data,'figureurl_qq_1'));
	        }elseif($type=='SINA'){
	            $data = $sns->call('users/show','uid='.My::arr2var($token,'openid'));
	            return array('openid'=>My::arr2var($data,'id'),'nickname'=>My::arr2var($data,'name'),'headimage'=>My::arr2var($data,'profile_image_url'));
	        }else{
	            $data =  $sns->call('sns/userinfo');
	            return array('openid'=>My::arr2var($data,'openid'),'nickname'=>My::arr2var($data,'nickname'),'headimage'=>My::arr2var($data,'headimgurl'),'unionid'=>My::arr2var($data,'unionid'));
	        }    
	    }
	    //获取第三方登录用户信息失败
	    else {
	        return false;
	    }
	}	
	

}